Telegram Group & Telegram Channel
🔍 C++ Задача для профи: кто вызовется?

У тебя есть следующий код:


#include <iostream>
#include <type_traits>

template<typename T>
typename std::enable_if<std::is_integral<T>::value, void>::type
process(T value) {
std::cout << "Integral: " << value << std::endl;
}

template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, void>::type
process(T value) {
std::cout << "Floating-point: " << value << std::endl;
}

int main() {
process(42); // (1)
process(3.14); // (2)
process('A'); // (3)
}


Вопросы:
1. Что напечатает программа?
2. Почему вызов process('A') может удивить?
3. Что произойдёт, если передать true?

🧠 Подвох:

- char — это integral type, а не string и не отдельный класс
- true — это тоже int`-подобный тип: `std::is_integral<bool>::value == true
- У тебя может возникнуть разный вывод в зависимости от перегрузки

Разбор:

- `process(42)` → `Integral: 42`
- `process(3.14)` → `Floating-point: 3.14`
- `process('A')` → `Integral: 65` (ASCII код символа!) ⚠️
- `process(true)` → `Integral: 1` (да, это `bool`)

🎯 Что проверяет задача:

- Понимание `SFINAE` и `enable_if`
- Типовую систему C++ (что именно считается integral)
- Разницу между `char`, `bool`, `int`, `float` в шаблонах
- Предсказание поведения перегрузки на уровне типов

@cpluspluc



tg-me.com/cpluspluc/1089
Create:
Last Update:

🔍 C++ Задача для профи: кто вызовется?

У тебя есть следующий код:


#include <iostream>
#include <type_traits>

template<typename T>
typename std::enable_if<std::is_integral<T>::value, void>::type
process(T value) {
std::cout << "Integral: " << value << std::endl;
}

template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, void>::type
process(T value) {
std::cout << "Floating-point: " << value << std::endl;
}

int main() {
process(42); // (1)
process(3.14); // (2)
process('A'); // (3)
}


Вопросы:
1. Что напечатает программа?
2. Почему вызов process('A') может удивить?
3. Что произойдёт, если передать true?

🧠 Подвох:

- char — это integral type, а не string и не отдельный класс
- true — это тоже int`-подобный тип: `std::is_integral<bool>::value == true
- У тебя может возникнуть разный вывод в зависимости от перегрузки

Разбор:

- `process(42)` → `Integral: 42`
- `process(3.14)` → `Floating-point: 3.14`
- `process('A')` → `Integral: 65` (ASCII код символа!) ⚠️
- `process(true)` → `Integral: 1` (да, это `bool`)

🎯 Что проверяет задача:

- Понимание `SFINAE` и `enable_if`
- Типовую систему C++ (что именно считается integral)
- Разницу между `char`, `bool`, `int`, `float` в шаблонах
- Предсказание поведения перегрузки на уровне типов

@cpluspluc

BY C++ Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/cpluspluc/1089

View MORE
Open in Telegram


C Academy Telegram | DID YOU KNOW?

Date: |

Tata Power whose core business is to generate, transmit and distribute electricity has made no money to investors in the last one decade. That is a big blunder considering it is one of the largest power generation companies in the country. One of the reasons is the company's huge debt levels which stood at ₹43,559 crore at the end of March 2021 compared to the company’s market capitalisation of ₹44,447 crore.

The STAR Market, as is implied by the name, is heavily geared toward smaller innovative tech companies, in particular those engaged in strategically important fields, such as biopharmaceuticals, 5G technology, semiconductors, and new energy. The STAR Market currently has 340 listed securities. The STAR Market is seen as important for China’s high-tech and emerging industries, providing a space for smaller companies to raise capital in China. This is especially significant for technology companies that may be viewed with suspicion on overseas stock exchanges.

C Academy from us


Telegram C++ Academy
FROM USA